All Questions
16 questions
-2votes
1answer
293views
Defining functions inside vs outside a class
Say I have a class with a function do_thing that is comprised of multiple steps, which themselves segregate into functions (first_process and second_process). At what point would this be considered ...
2votes
2answers
2kviews
Why access the attributes of a Python class by reference?
Attribute references and instantiation In this link, that is part of the official Python documentation, I have found the following information: Class objects support two kinds of operations: ...
3votes
3answers
315views
Referencing transient class attributes
I've just started dipping my feet into OOP. Is it considered bad practice to have classes that reference attributes that depend on another function being called and thus may not exist (version 1)? I'...
0votes
1answer
940views
Dependency Injection for dynamic objects
I am learning about Dependency Injection and I have been recently implementing the following classes for an app that executes commands over ssh using Python. I am confused about whether I am using it ...
-1votes
1answer
3kviews
Calling helper functions in a Python `__init__` function
Problem I am currently working with a class that necessarily has a very complicated initialization function (>350 lines of code) given that many computations and attributes need to be performed and ...
0votes
0answers
118views
"Best practice" or "design pattern" to group a class with "associated" classes in an object-oriented language
Sometimes a class A can have an "associated" class B such that the implementation of B depends on the implementation of A. For example, this can happen when B's objects are to be created by A's ...
2votes
2answers
4kviews
Module with globals or Class with attributes?
Currently I'm working with a lot of modules where the original developers used global variables to control states and to exchange important information between functions, like so: STATE_VAR = 0 def ...
-1votes
1answer
585views
How to write a loose Python interface where subclasses can add extra data?
Here are two object makers I made: def make_assassination(i): neighbors = [] def test(graph): for n in graph.neighbors(i): neighbors.append(n) ...
4votes
1answer
2kviews
Python class naming: nested classes or composed names?
I encountered a scenario where I cannot decide on which is the best (or worst) naming strategy. The context is the following: a bracket (as in a tournament) made up of nodes, where is node is made up ...
0votes
3answers
2kviews
When NOT to use a class / member variable?
I am trying to learn WHEN NOT to use: classes member variables HERE IS THE CODE access_point_detection_classes.py from scapy.all import * class Handler : def __init__(self) : self....
-1votes
2answers
110views
Object Oriented Python methods and their parameters
Let's say I have a class MyClass ... which has a data member x class MyClass1 : def __init__(self) : self.x = 1 Also a method which does something with x Should I pass self.x as a ...
1vote
3answers
2kviews
How To Extend Parent Methods in Children Classes?
There is a parent class with a method which many children use but many children extend the method, what is the best way to extend it without violating DRY? Here are my 2 current solutions: 1: The ...
1vote
2answers
848views
How should I structure these Python classes?
Base Class I have a class called Remote. This class represents a remote machine and has properties such as ip, hostname, username, and password, as well as methods for transferring files to/from the ...
14votes
3answers
41kviews
Python classes with only one instance: When to create a (single) class instance and when to work with the class instead?
Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of ...
0votes
1answer
1kviews
Should I use a class as a wrapper?
Lets say I have a class representing a chemical compound class Compound(networkx.Graph): def __init__(self): super(Compound, self).__init__() And lets say that I want to add some extra ...